package com.member.shop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.member.common.utils.DateUtils;
import com.member.shop.domain.SysBizRaffleActivity;
import com.member.shop.domain.SysBizRaffleActivityPrize;
import com.member.shop.domain.SysBizRafflePrize;
import com.member.shop.mapper.SysBizRaffleActivityMapper;
import com.member.shop.mapper.SysBizRaffleActivityPrizeMapper;
import com.member.shop.service.ISysBizRaffleActivityPrizeService;
import com.member.shop.service.ISysBizRaffleActivityService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 抽奖活动Service业务层处理
 *
 * @author lxc
 * @date 2024-03-13
 */
@Service
public class SysBizRaffleActivityServiceImpl extends ServiceImpl<SysBizRaffleActivityMapper, SysBizRaffleActivity> implements ISysBizRaffleActivityService {
    @Resource
    private SysBizRaffleActivityMapper sysBizRaffleActivityMapper;

    @Resource
    private ISysBizRaffleActivityPrizeService sysBizRaffleActivityPrizeService;

    @Resource
    private SysBizRaffleActivityPrizeMapper sysBizRaffleActivityPrizeMapper;

    /**
     * 查询抽奖活动
     *
     * @param id 抽奖活动主键
     * @return 抽奖活动
     */
    @Override
    public SysBizRaffleActivity selectSysBizRaffleActivityById(Long id) {
        SysBizRaffleActivity sysBizRaffleActivity = sysBizRaffleActivityMapper.selectById(id);
        List<SysBizRaffleActivityPrize> sysBizRaffleActivityPrizes = sysBizRaffleActivityPrizeMapper.selectListByActivityId(sysBizRaffleActivity.getId());
        sysBizRaffleActivity.setSysBizRaffleActivityPrize(sysBizRaffleActivityPrizes);
        return sysBizRaffleActivity;
    }

    /**
     * 查询抽奖活动列表
     *
     * @param queryWrapper 抽奖活动
     * @return 抽奖活动
     */
    @Override
    public List<SysBizRaffleActivity> selectSysBizRaffleActivityList(SysBizRaffleActivity queryWrapper) {
        return sysBizRaffleActivityMapper.selectSysBizRaffleActivityList(queryWrapper);
    }

    /**
     * 新增抽奖活动
     *
     * @param sysBizRaffleActivity 抽奖活动
     * @return 结果
     */
    @Override
    public int insertSysBizRaffleActivity(SysBizRaffleActivity sysBizRaffleActivity) {
        sysBizRaffleActivity.setCreateTime(DateUtils.getNowDate());
        int insert = sysBizRaffleActivityMapper.insert(sysBizRaffleActivity);


        sysBizRaffleActivity.getSysBizRaffleActivityPrize().parallelStream().forEach(obj -> obj.setActivityId(sysBizRaffleActivity.getId()));
        List<SysBizRaffleActivityPrize> sysBizRaffleActivityPrize = sysBizRaffleActivity.getSysBizRaffleActivityPrize();
        sysBizRaffleActivityPrizeService.saveBatch(sysBizRaffleActivityPrize);
        return insert;
    }

    /**
     * 修改抽奖活动
     *
     * @param sysBizRaffleActivity 抽奖活动
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSysBizRaffleActivity(SysBizRaffleActivity sysBizRaffleActivity) {
        sysBizRaffleActivity.setUpdateTime(DateUtils.getNowDate());

        //删除之前的
        SysBizRaffleActivityPrize sysBizRaffleActivityPrize = new SysBizRaffleActivityPrize();
        sysBizRaffleActivityPrize.setActivityId(sysBizRaffleActivity.getId());
        sysBizRaffleActivityPrize.setDeleted(1);
        sysBizRaffleActivityPrizeMapper.updateByActivityId(sysBizRaffleActivityPrize);

        //新增
        sysBizRaffleActivity.getSysBizRaffleActivityPrize().parallelStream().forEach(obj -> {
            obj.setActivityId(sysBizRaffleActivity.getId());
            obj.setId(null);
        });
        List<SysBizRaffleActivityPrize> sysBizRaffleActivityPrize1 = sysBizRaffleActivity.getSysBizRaffleActivityPrize();
        sysBizRaffleActivityPrizeService.saveBatch(sysBizRaffleActivityPrize1);

        return sysBizRaffleActivityMapper.updateById(sysBizRaffleActivity);
    }

    /**
     * 批量删除抽奖活动
     *
     * @param ids 需要删除的抽奖活动主键
     * @return 结果
     */
    @Override
    public int deleteSysBizRaffleActivityByIds(Long[] ids) {
        return sysBizRaffleActivityMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除抽奖活动信息
     *
     * @param id 抽奖活动主键
     * @return 结果
     */
    @Override
    public int deleteSysBizRaffleActivityById(Long id) {
        return sysBizRaffleActivityMapper.deleteById(id);
    }

    /**
     * 根据活动id获取奖品信息
     */
    @Override
    public List<SysBizRafflePrize> prizeByActivityId(Long activityId) {
        return sysBizRaffleActivityMapper.prizeByActivityId(activityId);
    }
}
